package com.situ.company.department.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.situ.company.department.dao.IDepartmentDao;
import com.situ.company.department.model.DepartmentModel;
import com.situ.company.util.FmtEmpty;
import com.situ.company.util.JDBCUtil;

// 数据层：sql+参数+映射
public class Department2DaoImpl implements IDepartmentDao{
	private static String table = "department";
	private static String cols ="code,name,tel";
	
	@Override
	public Integer insert(DepartmentModel model) {
		StringBuffer sql = new StringBuffer(" insert into ");
		sql.append(table).append('(').append(cols).append(")values(?,?,?)");
//		List<Object> values = Arrays.asList(model.getCode(),model.getName(),model.getTel());
//		return JDBCUtil.update(sql.toString(), values);
		return JDBCUtil.update(sql.toString(), model.getCode(),model.getName(),model.getTel());
	}

	@Override
	public Integer deleteByCode(DepartmentModel model) {
		StringBuffer sql = new StringBuffer(" delete from ");
		sql.append(table).append(" where code = ? ");
//		List<Object> values = Arrays.asList(model.getCode());
//		return JDBCUtil.update(sql.toString(), values);
		return JDBCUtil.update(sql.toString(), model.getCode());
	}

	@Override
	public Integer delete(DepartmentModel model) {
		StringBuffer sql = new StringBuffer(" delete from ").append(table);
		List<Object> values = appendWhere(sql, model);
		return JDBCUtil.update(sql.toString(), values);
	}

	@Override
	public Integer updateAll(DepartmentModel model) {
		StringBuffer sql = new StringBuffer(" update ");
		sql.append(table).append(" set name=?,tel=? where code=? " );
		return JDBCUtil.update(sql.toString(),model.getName(),model.getTel(),model.getCode());
	}

	@Override
	public Integer updateActive(DepartmentModel model) {
		StringBuffer sql = new StringBuffer(" update ");
		sql.append(table).append(" set id=id ");
		List<Object> values = appendSet(sql, model);
		return JDBCUtil.update(sql.toString(), values);
	}
	
	private List<Object> appendSet(StringBuffer sql,DepartmentModel model){
		List<Object> values = new ArrayList<>();
		String name = model.getName();
		if(name != null) {
			sql.append(" ,name=? ");
			values.add(name);
		}
		String tel = model.getTel();
		if(tel != null) {
			sql.append(" ,tel=? ");
			values.add(tel);
		}
		sql.append(" where code = ? ");
		values.add(model.getCode());
		return values;
	}

	@Override
	public List<DepartmentModel> selectList(DepartmentModel model) {
		StringBuffer sql = new StringBuffer(" select id , ").append(cols).append(" from ").append(table);
		List<Object> values = appendWhere(sql,model);
		return JDBCUtil.queryList(sql.toString(), values,DepartmentModel.class );
	}

	@Override
	public DepartmentModel selectModel(DepartmentModel model) {
		StringBuffer sql = new StringBuffer(" select id , ");
		sql.append(cols).append(" from ").append(table).append(" where code = ?");
		List<Object> values = Arrays.asList(model.getCode());
		return JDBCUtil.queryModel(sql.toString(),values,DepartmentModel.class);
	}
	
	private List<Object> appendWhere(StringBuffer sql, DepartmentModel model) {
		sql.append(" where 1 = 1 ");
		List<Object> values = new ArrayList<>();
		String code = model.getCode();
		if(!FmtEmpty.isEmpty(code)) {
			sql.append(" and code like ? ");
			values.add(code);
		}
		String name = model.getName();
		if(!FmtEmpty.isEmpty(name)) {
			sql.append(" and name like ? ");
			values.add(name);
		}
		String tel = model.getTel();
		if(!FmtEmpty.isEmpty(tel)) {
			sql.append(" and tel like ? ");
			values.add(tel);
		}
		
		if(model.isPageOn()) {
			sql.append(" limit ?,? ");
			//第一页十条记录:0,10
			//第二页十条记录:10,10
			//第三页十条记录:20,10
			//第pageIndex页pagelimit条记录:(pageIndex-1)*pagelimit , pagelimit
			values.add(model.getRowStart());
			values.add(model.getPageLimit());
		}
		return values;
	}

	@Override
	public Integer selectCount(DepartmentModel model) {
		StringBuffer sql = new StringBuffer(" select count(1) from department ");
		List<Object> vals = appendWhere(sql, model);
		return JDBCUtil.queryInt(sql.toString(), vals);
		
	}

}


